//
//  TargetPickerView.m
//  project
//
//  Created by 圆圆陈 on 2018/1/20.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "TargetPickerView.h"

@interface TargetPickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property(nonatomic, strong) NSString        *returnStr;
@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation TargetPickerView {
UIView       *backView;
UIPickerView *targetPicker;
}

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_returnStr               = [NSString string];
_array                   = [NSMutableArray array];
backView                 = [[UIView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT/2, HHHWIDTH, HHHHEIGHT/2)];
backView.backgroundColor = UIColorHEX(0xF0F0F6);
[self addSubview:backView];


UIView *view1111 = [[UIView alloc] initWithFrame:SNFRAME(0, 0, screenWidth, 44)];
view1111.backgroundColor = [UIColor whiteColor];
[backView addSubview:view1111];

UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cancelBtn.frame = CGRectMake(FitW(10), FitH(10), FitW(70), FitH(30));
[cancelBtn setTitleNormal:HHH_Text(@"运动目标")];
[cancelBtn setTitleColorNormal:SNCOLORBLACK];
//        [cancelBtn addTouchUpTarget:self action:@selector(cancelBtnClick)];
cancelBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
CGSize titleSize = [cancelBtn.titleLabel.text sizeWithFont:cancelBtn.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, FitH(30))];
cancelBtn.frame = CGRectMake(FitW(10), FitH(10), titleSize.width, FitH(30));
[backView addSubview:cancelBtn];

UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
sureBtn.frame = CGRectMake(HHHWIDTH-40, 10, 24, 24);
//        [sureBtn setTitleNormal:HHH_Text(@"确定")];
//        [sureBtn setTitleColorNormal:SNCOLORBLACK];
sureBtn.image = [UIImage imageNamed:@"icon／core／delete-text"];
[sureBtn addTouchUpTarget:self action:@selector(cancelBtnClick)];
[backView addSubview:sureBtn];


UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, FitH(50), HHHWIDTH, 1)];
line.backgroundColor = SNCOLORGRAYLINE;
//        [backView addSubview:line];

//        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(FitW(10), line.bottom, HHHWIDTH-FitW(20), FitH(20))];
//        label.text          = SN_Text(@"运动目标");
//        label.font          = [UIFont systemFontOfSize:FitFont(15)];
//        label.textAlignment = NSTextAlignmentLeft;
//        [backView addSubview:label];

UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(FitW(10), line.bottom, HHHWIDTH-FitW(20), 66)];
label2.text          = SN_Text(@"根据世界卫生组织建议，成人需要每天进行中等强度训练40分钟，相当于快走8000步");
    label2.font          = SNFONTREGULAR(13);
label2.numberOfLines = 0;
    label2.adjustsFontSizeToFitWidth = YES;
    label2.textColor = UIColorHEX(0x96969F);
label2.lineBreakMode = NSLineBreakByTruncatingTail;
label2.textAlignment = NSTextAlignmentLeft;
[backView addSubview:label2];

UIView *bgView = [[UIView alloc] initWithFrame:SNFRAME(15, 200, screenWidth-30, 50)];
bgView.backgroundColor     = [UIColor whiteColor];
bgView.layer.cornerRadius  = 6;
bgView.layer.masksToBounds = YES;
[backView addSubview:bgView];

UIView *sss = [[UIView alloc] initWithFrame:CGRectMake(0, label2.bottom+FitH(10) -15, HHHWIDTH, backView.height-label2.bottom-FitH(10)-40)];

bgView.center = CGPointMake(sss.center.x, sss.center.y);

targetPicker            = [[UIPickerView alloc] initWithFrame:CGRectMake(0, label2.bottom+FitH(10) -15, HHHWIDTH, backView.height-label2.bottom-FitH(10)-40)];
targetPicker.delegate   = self;
targetPicker.dataSource = self;
[backView addSubview:targetPicker];
for (int i = 1000; i <= 30000; i += 1000) {
    [_array addObject:[NSString stringWithFormat:@"%d", i]];
}

UIButton *btn = [[UIButton alloc] initWithFrame:SNFRAME(screenWidth/2-90, targetPicker.bottom -10, 180, 36)];
[btn setTitle:SN_Text(@"确定") forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.backgroundColor    = UIColorHEX(0xFF7A00);
btn.layer.cornerRadius = 6;
[backView addSubview:btn];
[btn addTouchUpTarget:self action:@selector(sureBtnClick)];


UILabel *unitLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, HHHWIDTH/2-40, 30)];
unitLabel.center    = CGPointMake(targetPicker.center.x+FitW(140)- 12, targetPicker.center.y);
unitLabel.text      = HHH_Text(@"步");
unitLabel.textColor = SNCOLORBLACK;
unitLabel.font      = [UIFont systemFontOfSize:13];
[backView addSubview:unitLabel];




UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelBtnClick)];
tap.delegate = self;
[self addGestureRecognizer:tap];
}
return self;
}

- (void)setTarget:(NSInteger)target {
[targetPicker selectRow:(target-1000)/1000 inComponent:0 animated:NO];
NSInteger index = (target-1000)/1000;
if (index < _array.count) {
_returnStr = [_array objectAtIndex:index];
}
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return _array.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return targetPicker.height/3;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;

if (!pickerLabel) {
pickerLabel               = [[UILabel alloc] init];
pickerLabel.font          = SNFONT(FitSimpleSize(20, 20, 30, 30, 30)-5);
pickerLabel.textColor     = SNCOLORBLACK;
pickerLabel.textAlignment = NSTextAlignmentCenter;
[pickerLabel setBackgroundColor:[UIColor clearColor]];
}
pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];

//在该代理方法里添加以下两行代码删掉上下的黑线
[[pickerView.subviews objectAtIndex:1] setHidden:YES];
[[pickerView.subviews objectAtIndex:2] setHidden:YES];
return pickerLabel;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [_array objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
_returnStr = [_array objectAtIndex:row];
}

- (void)cancelBtnClick {
[self animationedDown];
if (self.sureButtonBlock) {
self.sureButtonBlock(@"");
}
}

- (void)sureBtnClick {
if (self.sureButtonBlock) {
self.sureButtonBlock(_returnStr);
}
[self animationedDown];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isDescendantOfView:backView]) {
return NO;
}
return YES;
}

@end
